<!--
 * @Author: 蒋澳鹏 2210107952@qq.com
 * @Date: 2023-04-23 08:46:02
 * @LastEditors: 蒋澳鹏 2210107952@qq.com
 * @LastEditTime: 2023-05-04 11:22:12
 * @FilePath: \kaijin-web\src\views\informationScreen-index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div style="width: 100%;height: 100%">
    <informationScreenrow :dataInfo="dataInfo"
                          :type="type"
                          v-if="type === 1 || type === 2" />
    <informationScreencolumn :dataInfo="dataInfo"
                             :type="type"
                             v-if="type === 3 || type === 4" />
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import informationScreenrow from './informationScreen-row.vue'
import informationScreencolumn from './informationScreen-column.vue'
import axios from 'axios'
export default {
  components: { informationScreenrow, informationScreencolumn },
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        activatedIsNeed: false,
        getDataListURL: '', // 数据列表接口，API地址
        getTimeIntervalURL: '', // 获取时间间隔
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addOrUpdateURL: '', // 添加，API地址
        advertisementTypePageURL: '',
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      type: null, // 1:横屏1 2:横屏2 3:竖屏1 4:竖屏2
      dataInfo: {
        weather: {
          max: '',
          min: '',
          tips: '',
          url: '',
          type: '',
          humidity: '',
          wendu: '',
          kongqi: '',
          tigan: ''
        }, // 天气信息
        list: []
      },
      timer: null,
      deviceId: ''
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
    this.$on('removenowTime')
  },
  mounted () {
    let url = window.location.href
    let obj = this.queryURLParams(url)
    this.type = Number(obj.type)
    this.templateId = obj.templateId
    if (!this.type || !this.templateId) {
      return this.$message({
        showClose: true,
        duration: 0,
        message: '缺少启动参数,请确认后重试',
        type: 'error'
      })
    }
    this.type = Number(this.queryURLParams(url).type)
    // 立即执行一次，然后每小时执行一次
    if (this.type === 1 || this.type === 3) {
      this.getWeather()
    }
    this.getData()
    this.timer = setInterval(() => {
      this.getData()
      if (this.type === 1 || this.type === 3) {
        this.getWeather()
      }
    }, 1000 * 60 * 60)
  },
  methods: {
    getData () {
      this.$http.get('tb/templateIssue/programList?templateId=' + this.templateId).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        if (res.data) {
          this.dataInfo.list = res.data
        }
      })
    },
    /**
     * @description: 获取天气 每半小时执行一次
     * @example: res.data.forecast[0] 当天
     * @return {*}
     */
    getWeather () {
      axios.get('https://restapi.amap.com/v3/weather/weatherInfo?city=441900&key=c12ec8c0dee284dbfbc891f1eebab7c6&extensions=base').then(({ data: res }) => {
        this.dataInfo.weather.wendu = res.lives[0].temperature // 当前温度
        this.dataInfo.weather.humidity = res.lives[0].humidity // 湿度
        this.dataInfo.weather.type = res.lives[0].weather
        this.dataInfo.weather.url = require('@/assets/img/duoyun.png')
        if (this.dataInfo.weather.type.includes('晴') || this.dataInfo.weather.type.includes('热')) {
          this.dataInfo.weather.url = require('@/assets/img/qing.png')
        } else if (this.dataInfo.weather.type.includes('风') || this.dataInfo.weather.type.includes('阴')) {
          this.dataInfo.weather.url = require('@/assets/img/yin.png')
        } else if (this.dataInfo.weather.type.includes('云')) {
          this.dataInfo.weather.url = require('@/assets/img/duoyun.png')
        } else if (this.dataInfo.weather.type.includes('雷')) {
          this.dataInfo.weather.url = require('@/assets/img/leizhenyu.png')
        } else if (this.dataInfo.weather.type.includes('雨') || this.dataInfo.weather.type.includes('小雨')) {
          this.dataInfo.weather.url = require('@/assets/img/xiaoyu.png')
        } else if (this.dataInfo.weather.type.includes('中雨')) {
          this.dataInfo.weather.url = require('@/assets/img/zhongyu.png')
        } else if (this.dataInfo.weather.type.includes('大雨') || this.dataInfo.weather.type.includes('暴雨') || this.dataInfo.weather.type.includes('阵雨')) {
          this.dataInfo.weather.url = require('@/assets/img/dayu.png')
        } else if (this.dataInfo.weather.type.includes('雪') || this.dataInfo.weather.type.includes('冷')) {
          this.dataInfo.weather.url = require('@/assets/img/xue.png')
        } else if (this.dataInfo.weather.type.includes('雾') || this.dataInfo.weather.type.includes('霾') || this.dataInfo.weather.type.includes('沙') || this.dataInfo.weather.type.includes('尘')) {
          this.dataInfo.weather.url = require('@/assets/img/wu.png')
        }
      })
      axios.get('https://restapi.amap.com/v3/weather/weatherInfo?city=441900&key=c12ec8c0dee284dbfbc891f1eebab7c6&extensions=all').then(({ data: res }) => {
        this.dataInfo.weather.max = res.forecasts[0].casts[0].daytemp // 最高温
        this.dataInfo.weather.min = res.forecasts[0].casts[0].nighttemp // 最低温
        this.dataInfo.weather.tips = res.forecasts[0].casts[0].dayweather === res.forecasts[0].casts[0].nightweather ? res.forecasts[0].casts[0].dayweather : res.forecasts[0].casts[0].dayweather + '转' + res.forecasts[0].casts[0].nightweather // 天气概状
      })
      if (location.href.split('/#/')[0].includes('192.168.0.221')) {
        axios.get('http://192.168.0.221:10035/weather_mini/api/weather/city/101281601').then(({ data: res }) => {
          this.dataInfo.weather.kongqi = res.data.quality
          this.dataInfo.weather.tigan = res.data.wendu
        })
      } else {
        axios.get('/rydemo1').then(({ data: res }) => {
          this.dataInfo.weather.kongqi = res.data.quality
          this.dataInfo.weather.tigan = res.data.wendu
        })
      }
    },
    queryURLParams (url) {
      let pattern = /(\w+)=(\w+)/ig // 定义正则表达式
      let parames = {} // 定义参数对象
      url.replace(pattern, ($, $1, $2) => {
        parames[$1] = $2
      })
      return parames
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
